<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class UploadController extends Controller
{
    /**
     * [uploadFile description]上传图片
     * @author wuyun
     * @date           2018-03-15T14:50:22+0800
     * @param  Request type img 一般图片 idcard 身份证
     * @param  Request session   登陆后获得的加密的key；用来验证
     * @param  Request file    上传的文件
     * @return [type]                           [description]
     */
    public function postUploadImg(Request $request){
        if ($request->hasFile('file')){
            $file = $request->file('file');
            if ($file->isValid()) {

                 // 获取文件相关信息
                $originalName = $file->getClientOriginalName(); // 文件原名
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                $type = $file->getClientMimeType();     // image/jpeg
                $size = $file->getSize();     // image/jpeggetSize

                if ($ext === false) {
                    return $this->error('无效的图片文件');
                }
                if (!in_array($ext, ['jpg', 'jpeg', 'png','gif'])) {
                    return $this->error('无效的图片格式');
                }

                // 上传文件
                if ($size > 1048576) {
                    return $this->error('图片过大；上传文件不可超过1MB');
                }

                $target_path = 'upload/' . date('Y/m/d/');
                $filename = date('YmdHis') . '-' . uniqid() . '.' . $ext;
                $targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
                if (!is_dir($targetPath)) {
                    if (!mkdir($targetPath, 0777, 1)) {
                        return $this->error('无法建立上传目录');
                    }
                }
                if($file->move($target_path,$filename)){
                    return $this->response($target_path.$filename);
                }else{
                    return $this->error('上传失败');
                }
            }else{
                return $this->error('上传失败');
            }
        }
        return $this->error('无文件上传');
    }

    public function postUploadBase(Request $request)
    {
        $file = $request->input('imgBase64');
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)){
            $type = strtolower($result[2]);
            if (!in_array($type, ['png', 'jpg', 'jpeg','gif','bmp'])) {
                return $this->error('无效的图片格式');
            }

            $target_path = 'upload/' . date('Y/m/d/');
            $targetPath = public_path() . DIRECTORY_SEPARATOR . $target_path;
            if (!is_dir($targetPath)) {
                if (!mkdir($targetPath, 0777, 1)) {
                    return $this->error('无法建立上传目录');
                }
            }
            $new_filename = uniqid(mt_rand()) . '.'.$type;
            if (file_put_contents($target_path . $new_filename, base64_decode(str_replace($result[1], '', $file)))){
                return $this->response($target_path . $new_filename);
            }else{
                return $this->error('保存文件失败');
            }

        }else{
            return $this->error('无效的文件');
        }
    }
}
